package com.niiwoo.civet.account.service.local.openAccount2.service;

import com.niiwoo.civet.account.constant.AccountRegisterErrorCode;
import com.niiwoo.civet.account.dao.entity.BankCard;
import com.niiwoo.civet.account.dao.entity.GroupAccountCorrelateOrder;
import com.niiwoo.civet.account.dao.entity.OpenAccountRecord;
import com.niiwoo.civet.account.dao.mapper.BankCardMapperExt;
import com.niiwoo.civet.account.enums.AccountBaseIsShowBillEnum;
import com.niiwoo.civet.account.enums.AccountTypeEnum;
import com.niiwoo.civet.account.enums.OpenAccountRecordStatusEnum;
import com.niiwoo.civet.account.service.local.openAccount2.dto.AccountInfo;
import com.niiwoo.civet.account.service.local.openAccount2.dto.CreateRequest;
import com.niiwoo.civet.account.service.local.openAccount2.dto.UpdateRequest;
import com.niiwoo.civet.account.service.local.openAccount2.type.GroupTypeable;
import com.niiwoo.tripod.lanmao.enums.GroupAccountCorrelateTypeEnum;
import com.niiwoo.tripod.provider.exception.BizException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class GroupOpenAccountService extends AbstractOpenAccountService<GroupAccountCorrelateOrder> implements GroupTypeable {

    @Autowired
    private BankCardMapperExt bankCardMapperExt;


    @Override
    protected void innerCreateParamValid(CreateRequest request) {

    }

    @Override
    protected void innerCreateRecordValid(CreateRequest request, List<OpenAccountRecord> recordList) {
        // 查询个人绑卡注册成功记录
        Optional<OpenAccountRecord> preSuccessRecord = recordList.stream().filter(r -> Objects.equals(r.getStatus(), OpenAccountRecordStatusEnum.SUCCESS.getValue())).findFirst();
        if (!preSuccessRecord.isPresent()) {
            throw new BizException(AccountRegisterErrorCode.SUCCESS_RECORD_NOT_EXISTS);
        }
    }

    @Override
    protected AccountInfo innerUpdateAccountInfo(OpenAccountRecord record, GroupAccountCorrelateOrder order, UpdateRequest request) {
        AccountInfo accountInfo = new AccountInfo();
        accountInfo.setAccountNo(record.getAccountNo());
        accountInfo.setRole(record.getAccountRole());
        accountInfo.setUserId(record.getUserId());
        // 注册并关联, 需要创建账户信息
        if (order.getCorrelateType().equals(GroupAccountCorrelateTypeEnum.REGISTER_AUTH.getValue())) {
            BankCard bankCard = bankCardMapperExt.selectSimpleUserBankCard(record.getUserId());
            accountInfo.setName(bankCard.getRealName());
            accountInfo.setIdCardNo(bankCard.getIdCard());
        }
        accountInfo.setAuthList(order.getAuthList());
        accountInfo.setAccountType(AccountTypeEnum.PERSONAL);
        accountInfo.setOrgId(record.getOrgId());
        accountInfo.setIsShowBill(isNiiWooAccount(record.getOrgId())? AccountBaseIsShowBillEnum.YES.getValue():AccountBaseIsShowBillEnum.NO.getValue());
        return accountInfo;
    }

}
